<?php
	
	/* Includes */
	require_once "/../Data/Html.php";
	
	/* Handles all views in the login module */
	class LoginView extends Html
	{
		/* Member variables */
		private static $login = "login";
		private static $logout = "logout";
		
		private static $username = "username";
		private static $userValue = "";
		private static $password = "password";
		private static $passValue = "";
		
		private static $rememberMe = "RememberMe";
		private static $checked = "";
		
		private static $userCookie = "userCookie";
		private static $passCookie = "passCookie";
		private static $rememberUser = "remember";
		
		private $loggedIn = "loggedIn";
		private $day = 86400;
		private $backInTime = 3600;
		
		public function GetUserValue() { return self::$userValue; }
		public function GetPassValue() { return self::$passValue; }
		
		/* Generates and returns a header in HTML format */
		public function DoHeader($isLoggedIn, $headerSize = "2")
		{
			return $isLoggedIn ?
				   Html::Div(Html::H("Logout", $headerSize, "loginHeader"), "headerDiv") :
				   Html::Div(Html::H("Login", $headerSize, "loginHeader"), "headerDiv");
		}
		
		/* Generates and reutrns a login form in HTML format */
		public function DoLoginBox()
		{
			self::$userValue = (isset($_COOKIE[self::$userCookie])) ? $_COOKIE[self::$userCookie] : "";
			self::$passValue = (isset($_COOKIE[self::$passCookie])) ? $_COOKIE[self::$passCookie] : "";
			self::$checked = (isset($_COOKIE[self::$rememberUser])) ? "checked" : "";
			
			return Html::div(
				"<form method='post' action=''>
					<div class='loginInputDiv'>
						<div class='inputDiv'>
							<img src='Gfx/user.svg' alt='user' width='32' height='32' class='inputIcon' />
							<input type='text'
							name='" . self::$username . "'
							value='" . self::$userValue . "'
							placeholder='Username..'
							class='loginNameBox' />
						</div>
						<div class='inputDiv'>
							<img src='Gfx/keyhole.svg' alt='password' width='32' height='32' class='inputIcon'/>
							<input type='password'
							name='" . self::$password . "'
							value='" . self::$passValue . "'
							placeholder='Password..'
							class='loginPassBox' />
						</div>
						<div>
							<input type='submit' value='login' name='" . self::$login . "' id='loginButton'/>
							<label>Remember me</label>
							<input type='checkbox' name='" . self::$rememberMe . "' " . self::$checked . "/>
						</div>
					</div>
				</form>", "loginDiv");
		}
		
		/* Generates and reutrns a logout button in HTML format */
		public function DoLogoutBox()
		{
			return Html::div(
				"<form method='post'>
					<input type='submit' value='logout' name='" . self::$logout . "'/>
				</form>", "logoutDiv");
		}
		
		/* Shows wether the user is logged in or not */
		public function DoLoginStatus($isLoggedIn)
		{
			return Html::Div($isLoggedIn ? "Logged in." : "Not Logged in.");
		}
		
		/* Shows wether the user succesfully logged in or not */
		public function DoLoginFeeback($isLoggedIn)
		{
			//return Html::Div($isLoggedIn? "You have Logged in." : "Wrong username or password.", "loginFeedback");
			if ($isLoggedIn)
				return Html::Div("You have Logged in.", "loginSuccess");
			else
				return Html::Div("Wrong username or password.", "loginFail");
		}
		
		/* Gets userinput from the username field */
		public function GetUserName()
		{
			if (isset($_POST[self::$username]))
				return $_POST[self::$username];
			
			return NULL;
		}
		
		/* Gets userinput from the password field */
		public function GetPassword()
		{
			if (isset($_POST[self::$password]))
				return $_POST[self::$password];
			
			return NULL;
		}
		
		/* Returns a value indicating if the user tried to log in TRUE/FALSE */
		public function TriedToLogIn()
		{
			return isset($_POST[self::$login]);
		}
		
		/* Returns a value indicating if the user tried to log out TRUE/FALSE */
		public function TriedToLogOut()
		{
			return isset($_POST[self::$logout]);
		}
		
		/* Returns a value indicating if the user wants to be remembered or not TRUE/FALSE */
		public function TriedToBeRemembered()
		{
			return isset($_POST[self::$rememberMe]);
		}
		
		/* Returns a value that indicates if a user is already remembered */
		public function IsRemembered()
		{
			return isset($_COOKIE[self::$userCookie]);
				/*return false;
			
			return ($this->GetUserName() == $_COOKIE[self::$userCookie]);*/
		}
		
		/* Remembers a user */
		public function RememberUser()
		{
			$this->ForgetUser();
			setcookie(self::$userCookie, $this->GetUserName(), time() + $this->day);
			setcookie(self::$passCookie, $this->GetPassword(), time() + $this->day);
		}
		
		/* Forgets a user */
		public function ForgetUser()
		{
			setcookie(self::$userCookie, "", time() - $this->backInTime);
			setcookie(self::$passCookie, "", time() - $this->backInTime);
		}
		
		/* Set the cookie that auto-checks the "Remember me" button to true */
		public function CheckRemembered()
		{
			setcookie(self::$rememberUser, "true", time() + $this->day);
		}
		
		/* Delete the cookie that keeps track of the "Remember me" button */
		public function UnCheckRemembered()
		{
			setcookie(self::$rememberUser, "", time() - $this->backInTime);
		}
	}

?>